export default {
    data () {
        return {
            xhrPending: false,
        }
    },
    methods: {
        jax (url, data, option = {loading: false, errmsg: '请求发生异常', ldmsg: '', errcb: null, method: 'POST'}) {
            option.loading && (this[option.loading] = true);

            return axios({
                url: host + url,
                withCredentials: true,
                data: data,
                method: option.method || 'POST'
            }).catch(e => {
                option.loading && (this[option.loading] = false);
                option.errcb && (typeof option.errcb === 'function') && option.errcb();
                this.msg('网络出现异常，请稍后再试', 'error');
                console.warn(e);
            }).then(res => {
                option.loading && (this[option.loading] = false);
                if (res.data.success) {
                    return res.data
                } else {
                    this.msg(res.data.message || option.errmsg, 'warning');
                    return res.data
                }
            })

        },
        get (url, data, option = {errmsg: '请求发生异常'}) {
            option.loading && (this[option.loading] = true);
            return axios.get(host + url, {
                withCredentials: true,
                params: data,
                headers: {
                    'Content-Type': 'application/json'
                },
                method: 'GET'
            })
                .then(res => {
                    option.loading && (this[option.loading] = false);
                    if (res.data.success) {
                        return res.data
                    } else {
                        this.msg(res.data.message || option.errmsg, 'warning');
                    }

                })
                .catch(e => {
                    this.msg('网络出现异常，请稍后再试', 'error');
                    console.warn(e);
                    option.loading && (this[option.loading] = false);
                })
        },
        msg (msg, type = 'info') {
            this.$message({
                message: msg,
                type: type
            })
        }
    }
}